package com.zhn.usermanager.downloadfile;

import com.zhn.usermanager.poi.POIUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 下载文件工具类
 */
@Component
public class MyExcelDownLoad {
    public void excelDownLoad(HttpServletResponse response) throws IOException {
        List<List<String>> arrayList_outer = new ArrayList<>();
        List<String> arrayList_inner = new ArrayList<>();
        arrayList_inner.add("A");
        arrayList_inner.add("B");
        arrayList_inner.add("C");
        arrayList_outer.add(arrayList_inner);

        arrayList_inner = new ArrayList<>();
        arrayList_inner.add("D");
        arrayList_inner.add("E");
        arrayList_inner.add("F");
        arrayList_outer.add(arrayList_inner);

        arrayList_inner = new ArrayList<>();
        arrayList_inner.add("G");
        arrayList_inner.add("H");
        arrayList_inner.add("I");
        arrayList_outer.add(arrayList_inner);

        POIUtil poiUtil = new POIUtil();
        Workbook workbook = poiUtil.creatExcelForPOI(arrayList_outer, "第一页");

        if(workbook != null) {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition","attachment; filename=" + System.currentTimeMillis() + ".xls");
            //获取响应报文输出流对象
            ServletOutputStream out = response.getOutputStream();
            //输出
            workbook.write(out);
            out.flush();
            out.close();
        }
        else {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print("系统服务出错!");
        }

    }
}
